import { Toast } from "vant";
import { isWechat } from "./validate";
export function last(data) {
	if (data != undefined && data != null) {
		return data[data.length - 1];
	}
}



export function randomNum(max) {
	return Math.ceil(Math.random() * max);
}

//timestamp是一个时间毫秒，注意时间戳是秒的形式，在这个毫秒的基础上除以1000，就是十位数的时间戳。13位数的都是时间毫秒。
export function timeAgo(timestamp) {
	var mistiming = Math.round((Date.now() - timestamp) / 1000);
	var arrr = ["年", "个月", "星期", "天", "小时", "分钟", "秒"];
	var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
	for (var i = 0; i < arrn.length; i++) {
		var inm = Math.floor(mistiming / arrn[i]);
		if (inm != 0) {
			return inm + arrr[i] + "前";
		}
	}
}
// 点击链接 判断是否有微信 有则打开
export function openWxApp(delay) {
	if (isWechat) {
		Toast({
			message: "您已在微信打开，返回粘贴添加微信好友！",
			position: "top"
		});
		return;
	}
	if (delay) {
		setTimeout(() => {
			window.location.href = "weixin://";
		}, 600);
		return;
	}
	window.location.href = "weixin://";
}

export function randomStr(length) {
	let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	let result = '';
	for (let i = length; i > 0; --i)
		result += str[Math.floor(Math.random() * str.length)];
	return result;
}

export function uuid() {
	let d = new Date().getTime();
	let uuid = "xxxxxxxxxxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, c => {
		let r = (d + Math.random() * 16) % 16 | 0;
		d = Math.floor(d / 16);
		return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
	});
	return uuid;
}